var addElement = function (view, parentNode) {
    var tree = Ext.getCmp('tree');
    var windowHeader;
    var parentElementLabel;
    var renderedItems;

    var name = Ext.create('Ext.form.TextField', {
        xtype: 'textfield',
        id: 'name',
        fieldLabel: 'Название',
        allowBlank: false,
        anchor: '100%'
    });

    var description = Ext.create('Ext.form.TextArea', {
        xtype: 'textarea',
        id: 'description',
        fieldLabel: 'Описание',
        allowBlank: true,
        anchor: '100%'
    });

    var lname = Ext.create('Ext.form.TextField', {
        xtype: 'textfield',
        id: 'lname',
        fieldLabel: 'Фамилия',
        allowBlank: false,
        anchor: '100%'
    });

    var fname = Ext.create('Ext.form.TextField', {
        xtype: 'textfield',
        id: 'fname',
        fieldLabel: 'Имя',
        allowBlank: false,
        anchor: '100%'
    });

    var pname = Ext.create('Ext.form.TextField', {
        xtype: 'textfield',
        id: 'pname',
        fieldLabel: 'Отчество',
        allowBlank: true,
        anchor: '100%'
    });

    var birthDate = Ext.create('Ext.form.DateField', {
        xtype: 'datefield',
        id: 'birthDate',
        format: 'd.m.Y',
        fieldLabel: 'Дата рождения',
        allowBlank: false,
        anchor: '100%'
    });

    var email = Ext.create('Ext.form.TextField', {
        xtype: 'textfield',
        id: 'email',
        fieldLabel: 'Эл. почта',
        allowBlank: false,
        vtype: 'email',
        anchor: '100%'
    });

    var jobPositionStore = Ext.create('Ext.data.Store', {
        fields: [ 'data' ],
        proxy: {
            type: 'ajax',
            url: 'employee/getJobPositions'
        }
    });

    var jobPosition = Ext.create('Ext.form.ComboBox', {
        xtype: 'combo',
        id: 'jobPosition',
        fieldLabel: 'Должность',
        displayField: 'data',
        valueField: 'data',
        allowBlank: false,
        editable: false,
        typeAhead: true,
        selectOnFocus: true,
        triggerAction: 'all',
        emptyText: 'Укажите должность...',
        anchor: '100%',
        store: jobPositionStore
    });

    if (parentNode.get('id').match(/Proj_\d*/)) {
        windowHeader = 'Новый сотрудник';
        parentElementLabel = 'Проект';
        renderedItems = [ lname, fname, pname, birthDate, email, jobPosition ];
    } else {
        renderedItems = [ name, description ];
        if (parentNode.get('id') == 'Root') {
            windowHeader = 'Новое подразделение';
            parentElementLabel = 'Компания';
        } else if (parentNode.get('id').match(/Div_\d*/)) {
            windowHeader = 'Новый проект';
            parentElementLabel = 'Подразделение';
        }
    }

    var parentElement = Ext.create('Ext.form.TextField', {
        xtype: 'textfield',
        id: 'parentElement',
        fieldLabel: parentElementLabel,
        value: parentNode.get('text'),
        anchor: '100%',
        readOnly: true
    });

    renderedItems.push(parentElement);

    var addActionForm = Ext.create('Ext.form.FormPanel', {
        baseCls: 'x-plain',
        defaultType: 'textfield',
        items: renderedItems
    });

    var addActionWindow = Ext.create('Ext.Window', {
        title: windowHeader,
        width: 500,
        resizable: false,
        plain: true,
        bodyStyle: 'padding: 5px 5px 0 5px; border: none;',
        buttonAlign: 'right',
        items: addActionForm,
        layout: 'fit',
        buttons: [
            {
                text: 'Добавить',
                handler: function () {
                    if (addActionForm.getForm().isValid()) {
                        addActionForm.getForm().submit({
                            url: 'structure/addElement',
                            params: {
                                node: parentNode.get('id')
                            },
                            waitMsg: 'Пожалуйста, ожидайте',
                            success: function (form, action) {
                                addActionWindow.close();
                                tree.getStore().read({node: parentNode});
                                Ext.Msg.alert('ОТЧЕТ', action.result.message);
                            },
                            failure: function (form, action) {
                                if (action.failureType === Ext.form.action.Action.CONNECT_FAILURE) {
                                    Ext.Msg.alert('ОШИБКА', 'Ошибка соединения с сервером');
                                } else if (action.failureType === Ext.form.action.Action.SERVER_INVALID) {
                                    Ext.Msg.alert('ОШИБКА', action.result.message);
                                }
                            }
                        });
                    }
                }
            },
            {
                text: 'Отмена',
                handler: function () {
                    addActionForm.getForm().reset();
                    addActionWindow.close();
                }
            }
        ]
    });

    addActionWindow.show();
};